package mango.functions;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import mango.Mango;

@SuppressWarnings("unchecked")
public final class FuncServlet {
    public static void printRequest(HttpServletRequest request) {
        Mango.getLog().trace(request.getClass());
        Mango.getLog().trace("request.getAuthType()|" + request.getAuthType());
        Mango.getLog().trace("request.getCharacterEncoding()|" + request.getCharacterEncoding());
        Mango.getLog().trace("request.getContentLength()|" + request.getContentLength());
        Mango.getLog().trace("request.getContentType()|" + request.getContentType());
        Mango.getLog().trace("request.getContextPath()|" + request.getContextPath());
        Mango.getLog().trace("request.getLocalAddr()|" + request.getLocalAddr());
        Mango.getLog().trace("request.getLocalName()|" + request.getLocalName());
        Mango.getLog().trace("request.getLocalPort()|" + request.getLocalPort());
        Mango.getLog().trace("request.getMethod()|" + request.getMethod());
        Mango.getLog().trace("request.getPathInfo()|" + request.getPathInfo());
        Mango.getLog().trace("request.getPathTranslated()|" + request.getPathTranslated());
        Mango.getLog().trace("request.getProtocol()|" + request.getProtocol());
        Mango.getLog().trace("request.getQueryString()|" + request.getQueryString());
        Mango.getLog().trace("request.getRemoteAddr()|" + request.getRemoteAddr());
        Mango.getLog().trace("request.getRemoteHost()|" + request.getRemoteHost());
        Mango.getLog().trace("request.getRemoteUser()|" + request.getRemoteUser());
        Mango.getLog().trace("request.getRemotePort()|" + request.getRemotePort());
        Mango.getLog().trace("request.getRequestedSessionId()|" + request.getRequestedSessionId());
        Mango.getLog().trace("request.getRequestURI()|" + request.getRequestURI());
        Mango.getLog().trace("request.getScheme()|" + request.getScheme());
        Mango.getLog().trace("request.getServerName()|" + request.getServerName());
        Mango.getLog().trace("request.getServerPort()|" + request.getServerPort());
        Mango.getLog().trace("request.getServletPath()|" + request.getServletPath());
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String header = headerNames.nextElement();
            Mango.getLog().trace("request.getHeader(" + header + ")|" + request.getHeader(header));
        }
    }
    
    public static final String getServer(HttpServletRequest request) {
        StringBuilder server = new StringBuilder();
        server.append(request.getScheme()).append("://").append(request.getHeader("host"))
                .append(request.getContextPath());
        return server.toString();
    }
    
    public static String getRequestActionName(HttpServletRequest request) {
        String action = request.getServletPath();
        if (action == null || FuncString.isBlank(action)) {
            return null;
        }
        action = action.trim();
        if (action.startsWith("/")) {
            action = action.substring(1);
        }
        if (action.indexOf(".") > 0) {
            action = action.substring(0, action.indexOf("."));
        }
        String[] temp = action.split("/");
        temp[temp.length - 1] = FuncString.firstUpper(temp[temp.length - 1]);
        action = FuncString.margeString(temp, ".");
        return "." + action;
    }
}
